package com.android.qjpmc.data_module;

import com.android.qjpmc.constant.Constant;

import java.util.ArrayList;
import java.util.List;

public class RtDataEntranceMnager {
    //接线图数据入口
    public List<RTDataEntrance> m_JxtRtDataEntranceList = new ArrayList<RTDataEntrance>();

    //实时设备数据entrance
    RTDataEntrance m_DeviceRTDataEntrance = new RTDataEntrance();

    //当前的entrance,可以是设备的,也可能是接线图的, 也可能是实时曲线的.
    RTDataEntrance m_CurrentEntrance = null;


    public void SetCurrentEntrance(RTDataEntrance entrance)
    {
        RTDataEntrance OldEntrance = m_CurrentEntrance;
        m_CurrentEntrance = entrance;
        if ( (m_CurrentEntrance != null)  )
        {
            m_CurrentEntrance.GeneratePointCodeList();
        }
    }

    RTDataEntrance GetJxtEntrance(int ID)
    {
        int Size = m_JxtRtDataEntranceList.size();
        RTDataEntrance entrance = null;
        if (Size == 0)
        {
             return null;
        }
        if (ID == 0)
        {
            return m_JxtRtDataEntranceList.get(0);
        }
        for (int idx = 0; idx < Size; idx++)
        {
            entrance = m_JxtRtDataEntranceList.get(idx);
            if (entrance != null)
            {
                if (entrance.IsMe(ID) == Constant.TRUE)
                {
                    return entrance;
                }
            }
        }
        return null;
    }

    //设定当前数据接口
    public void SetCurrentRtDataEntrance(int JxtID)
    {
        RTDataEntrance entrance = GetJxtEntrance(JxtID);
        SetCurrentEntrance(entrance);
    };


    public void ClearCurrentRtDataEntrance()
    {
        SetCurrentEntrance(null);
    }

    public RTDataEntrance GetCurrentRtDataEntrance()
    {
        return m_CurrentEntrance;
    }

    //设定当前设备实时数据入口,当实时更新某一设备的时候,调用该函数....
    public int SetCurrentRtDataEntrance(PwrDevice device)
    {
        if (device == null)
        {
            return Constant.GEN_ERROR;
        }
        ArrayList<AnalogDataItem> list =  device.GetAnalogDataList();
        //清除entrance中的数据
        m_DeviceRTDataEntrance.Clear();
        if (list != null)
        {
            int count = list.size();
            AnalogDataItem item = null;
            for (int idx = 0; idx < count; idx++)
            {
                item = list.get(idx);
                if (item != null)
                {
                    m_DeviceRTDataEntrance.AddNode(item.m_TagName, item);
                }
            }
        }
        //设定当前entrance
        SetCurrentEntrance(m_DeviceRTDataEntrance);
        return Constant.NO_ERROR;
    }

}
